package com.yu.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * Created by xiaolongyu on 15/3/27.
 */
public class AnnotationTest {

    public static void main(String[] args) {
//        test1();
        test2();
    }

    public static void test1(){
        Class<AnnotationClass> object = AnnotationClass.class;
        Annotation[] annotations = object.getAnnotations();
        for(Annotation annotation : annotations){
            System.out.println(annotation);
        }

        MyAnnotation annotation = object.getAnnotation(MyAnnotation.class);
        System.out.println("annotation:" + annotation.autor());

//        Method[] methods = object.getMethods();
        Method[] methods = object.getDeclaredMethods();
        for(Method method : methods){
            System.out.println("method:" + method + " " + method.isAnnotationPresent(MyMethodAnnotation.class));
        }
    }

    public static void test2(){
        InheritedAnnotation annotation = InheritedClass.class.getAnnotation(InheritedAnnotation.class);
        System.out.println(annotation);

        MyAnnotation annotation1 = InheritedClass.class.getAnnotation(MyAnnotation.class);
        System.out.println(annotation1);
        System.out.println(InheritedSubClass.class.isAnnotationPresent(MyAnnotation.class));
        System.out.println(InheritedSubClass.class.isAnnotationPresent(InheritedAnnotation.class));

        InheritedSubClass subClass = new InheritedSubClass();
        InheritedAnnotation annotation2 = subClass.getClass().getAnnotation(InheritedAnnotation.class);
        System.out.println(annotation2);
    }
}
